unit uDataAccessObject_ADO;

interface

uses
  SysUtils, Classes, Forms, uDataAccessObject, ADODB, DB;

type
  TDataAccessObject_ADO = class(TDataAccessObject)
  private
    FConnection: TADOConnection;
    FDataSet: TADOQuery;
  protected
    procedure SetConnection; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure BeginTransaction; override;
    procedure CommitTransation; override;
    function ExecuteSQL(AQueryString: string): Boolean; override;
    function GetDataSet: TDataSet; override;
    procedure RollbackTransaction; override;
  end;

implementation

uses uServiceLocator_Intf, uLogManager_Intf;

const
  MSACCESS_CONNECTIONSTRING = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Persist Security Info=False';
  DATABASE_FILENAME = 'database\db.mdb';

{$R *.dfm}

{ TDataAccessObject_ADO }

{
**************************** TDataAccessObject_ADO *****************************
}
constructor TDataAccessObject_ADO.Create(AOwner: TComponent);
begin
  inherited;
  FConnection:= TADOConnection.Create(nil);
  FDataSet:= TADOQuery.Create(nil);
  FDataSet.Connection:= FConnection;
  SetConnection;
end;

destructor TDataAccessObject_ADO.Destroy;
begin
  if FDataSet.Active then
    FDataSet.Close;
  FreeAndNil(FDataSet);

  if FConnection.Connected then
    FConnection.Close;
  FreeAndNil(FConnection);
  inherited;
end;

procedure TDataAccessObject_ADO.BeginTransaction;
begin
  inherited;
  FConnection.BeginTrans;
end;

procedure TDataAccessObject_ADO.CommitTransation;
begin
  inherited;
  FConnection.CommitTrans;
end;

function TDataAccessObject_ADO.ExecuteSQL(AQueryString: string): Boolean;
begin
  Result:= True;
  try
    if FDataSet.Active then
      FDataSet.Active:= False;
    FDataSet.SQL.Text:= AQueryString;
    ServiceLocator.LogManager.Log('DataAccessObject_ADO.ExecuteSQL->' + AQueryString);
    if SameText(Copy(AQueryString, 1, 6), 'SELECT') then
      FDataSet.Open
    else
      FDataSet.ExecSQL;
  except
    Result:= False;
  end;
end;

function TDataAccessObject_ADO.GetDataSet: TDataSet;
begin
  Result:= FDataSet;
end;

procedure TDataAccessObject_ADO.RollbackTransaction;
begin
  inherited;
  FConnection.RollbackTrans;
end;

procedure TDataAccessObject_ADO.SetConnection;
begin
  with FConnection do
  begin
    if Connected then
      Close;
    ConnectionString:= Format(MSACCESS_CONNECTIONSTRING, [ExtractFilePath(Application.ExeName) + DATABASE_FILENAME]);
    Connected:= True;
  end;
end;

end.


